//========================================================================
// *?????????è  ?????ilplay.com
// *?????UIFrameMessage
// *??????UI??????????????????
// *?2021 MYJL . All rights reserved.
//* ======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Task = System.Threading.Tasks.Task;
using System.Threading;

namespace HDMCode.UIFrame
{
    public class UIFrameMessage
    {
        private static UIFrameMessage _instance;
        public static UIFrameMessage Get { get { if (_instance == null) { _instance = new UIFrameMessage(); } return _instance; } }
        /// <summary>
        /// ?????????????
        /// </summary>
        private Queue<UIFrameMessageStruct> uiMessageQueue { get; set; } = new Queue<UIFrameMessageStruct>();

        /// <summary>
        /// ????????????????
        /// </summary>
        private CancellationTokenSource taskToken;

        /// <summary>
        /// UI??????????????
        /// </summary>
        public UIMessageAction uiMessageAction;

        /// <summary>
        /// ????UI???
        /// </summary>
        public void AddUIMessage(UIFrameMessageStruct message)
        {
            if (!(message is null)) 
                uiMessageQueue.Enqueue(message);
        }

        /// <summary>
        /// ??????UI???
        /// </summary>
        public void ReadOnceMessage()
        {
            if (uiMessageQueue.Count > 0)
            {
#if(UNITY_WEBGL)
                PushUIMessage(uiMessageQueue.Dequeue());
#else
                lock (uiMessageQueue)
                    PushUIMessage(uiMessageQueue.Dequeue());
#endif
            }
        }

        /// <summary>
        /// ???????UI???
        /// </summary>
        public void ReadAllMessage()
        {
            while (uiMessageQueue.Count > 0)
            {
#if(UNITY_WEBGL)
                PushUIMessage(uiMessageQueue.Dequeue());
#else
                lock (uiMessageQueue)
                    PushUIMessage(uiMessageQueue.Dequeue());
#endif
            }
        }

        /// <summary>
        /// ??????????????UI
        /// </summary>
        /// <param name="message"></param>
        private void PushUIMessage(UIFrameMessageStruct message)
        {
            if (!(uiMessageAction is null)) 
            {
                uiMessageAction(message);
            }
        }

        /// <summary>
        /// ??????????UI??????????
        /// </summary>
        public (List<Type> Class, List<MethodInfo> Method) GetAllBaseUIMessageScript()
        {
            List<Type> classList = new List<Type>();
            List<MethodInfo> methodList = new List<MethodInfo>();
            Type[] types = typeof(IUIDisplayLife).Assembly.GetTypes();
            foreach (var classTypeItem in types)
            {
                Type curType = classTypeItem;
                Type baseType = classTypeItem.BaseType;

                if (Type.Equals(baseType, typeof(IUIDisplayLife)))
                {
                    classList.Add(classTypeItem);
                    methodList = curType.GetMethods().ToList<MethodInfo>();
                }
            }
            return (classList, methodList);
        }

#if (UNITY_IOS || UNITY_ANDROID || UNITY_EDITOR)

        /// <summary>
        /// ????????????????,?????????????????????????????????У??????????Э?????????????н???????????
        /// </summary>
        public void CreatPushTask()
        {
            taskToken = new CancellationTokenSource();
            var taskFuncion = Task.Factory.StartNew(
                async () =>
                {
                    while (true)
                    {
                        ReadOnceMessage();
                        await Task.Delay(10);
                    }
                },
                taskToken.Token);
        }

        /// <summary>
        /// ?????????????????????
        /// </summary>
        public void ClosePushTask()
        {
            taskToken.Cancel();
            taskToken.Dispose();
        }
#endif
    }

}